package com.tengju.bff.infrastructure.config;

import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @author WenLei 2020年5月13日.
 */

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer
                .addPathPrefix("api", c -> c.getPackageName().startsWith("com.tengju.bff.interfaces.app")
                        || c.getPackageName().startsWith("com.tengju.bff.interfaces.login")
                        || c.getPackageName().startsWith("com.tengju.bff.interfaces.data"))
                .addPathPrefix("boss", c -> c.isAnnotationPresent(ManageEntrance.class));
    }


    /**
     * 解决token失效回传code时的跨域问题
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")//设置允许跨域的路径
                .allowedOrigins("*")//设置允许跨域请求的域名
                .allowCredentials(true)//是否允许证书 不再默认开启
                .allowedHeaders("Access-Control-Allow-Headers", "*")
                .allowedHeaders("Access-Control-Allow-Origin", "*")
                .allowedHeaders("Access-Control-Allow-Methods", "*")
                .allowedMethods("*")
                .maxAge(3600);
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {

    }


    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
    }


}
